package com.dldata.devtools30.controller.metadata;

import com.dldata.devtools30.common.EnumUtils;
import com.dldata.devtools30.controller.DevBase30Controller;
import com.dldata.devtools30.entity.DevtModelMetadataTableEntity;
import com.dldata.devtools30.entity.DevtModelMetadataTypeEntity;
import com.dldata.devtools30.service.metadata.DevtModelMetadataTableService;
import com.dldata.devtools30.service.metadata.DevtModelMetadataTypeService;
import com.dldata.devtools30.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/V_DevtModelMetadataTableController")
public class V_DevtModelMetadataTableController extends DevBase30Controller {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    DevtModelMetadataTableService metadataTableService;
    @Autowired
    DevtModelMetadataTypeService metadataTypeService;

    @RequestMapping("toList")
    public String toList(Model model){
        EnumUtils.ModelTypeEnum[] modelTypeEnums = EnumUtils.ModelTypeEnum.values();
        ArrayList arrayList=new ArrayList();
        for (int i = 0; i < modelTypeEnums.length; i++) {
            EnumUtils.ModelTypeEnum modelTypeEnum = modelTypeEnums[i];
            HashMap hashMap=new HashMap();
            hashMap.put("value",modelTypeEnum.name());
            arrayList.add(hashMap);
        }
        model.addAttribute("modelTypeList",arrayList);
        return "thymeleaf/devtools30/metadata/v_MetadataTable";
    }

    @RequestMapping("edit")
    public String edit(String id,Model model){
        id= StringUtil.isEmptyOrLength0(id)? UUID.randomUUID().toString():id;
        List<DevtModelMetadataTableEntity> metadataTableEntities= metadataTableService.findById(id);
        DevtModelMetadataTableEntity metadataTableEntity =new DevtModelMetadataTableEntity();
        if(metadataTableEntities.size()>0){
            metadataTableEntity = metadataTableEntities.get(0);
        }else{
            List<DevtModelMetadataTableEntity> alldata = commonApiDao.getScrollData(DevtModelMetadataTableEntity.class).getResultlist();
            int xh = alldata.size() + 1;
            for (int i = 0; i < alldata.size(); i++) {
                DevtModelMetadataTableEntity entity = alldata.get(i);
                xh=entity.getXh()>=xh?(entity.getXh()+1):xh;
            }
            metadataTableEntity.setXh(xh);
        }
        model.addAttribute("data", metadataTableEntity);

        List<DevtModelMetadataTypeEntity>  metadataTypeEntities = metadataTypeService.getAll();

        model.addAttribute("ftypeList",metadataTypeEntities);
    
        EnumUtils.ModelTypeEnum[] modelTypeEnums = EnumUtils.ModelTypeEnum.values();
        ArrayList arrayList=new ArrayList();
        for (int i = 0; i < modelTypeEnums.length; i++) {
            EnumUtils.ModelTypeEnum modelTypeEnum = modelTypeEnums[i];
            HashMap hashMap=new HashMap();
            hashMap.put("value",modelTypeEnum.name());
            arrayList.add(hashMap);
        }
        model.addAttribute("modelTypeList",arrayList);

        return "thymeleaf/devtools30/metadata/v_MetadataTableEdit";
    }



}
